<template>
	<div class="home-ctr">
		<div class="header">
			<h2>{{user.name}}</h2>
			<span>{{user.mobile}}</span>
		</div>
		<div class="section">
			<div class="section-container">
				<com-item-enterprise :items='enterprisesItems'></com-item-enterprise>
			</div>
		</div>
	</div>
</template>

<script>

	import ComItemEnterprise from "@/components/item_enterprise";

	export default {

	  	name: 'home',

	  	components: {
	  		ComItemEnterprise
	  	},

	  	data () {
	    	return {
	    		user: {},
	    		enterprisesItems: null
	    	};
	  	},

	  	created () {
	  		this.getList().then(
	  			res => {
	  				this.getItem(res);
	  			}
	  		);
	  		fxAPI.api('setAppTitle', {title: '企业通讯录管理后台'});
	  	},

	  	methods: {
	  		/**
	  		 * 获取用户所在企业列表
	  		 * @return {[type]} [description]
	  		 */
	  		getList () {
	  			// this.enterprisesItems = this.cache.enterprises;
	  			// return;
	  			return new Promise((resolve, reject) => {
	  				this.$http.post(this.api.eab, {
	  					module: 'enterprise',
	  					action: 'getListWithExtraByUserId'
	  				}, {showLoading: true}).then(
	  					res => {
	  						if (res.body.result === 1){
	  							this.cache.enterprises = this.enterprisesItems = res.body.data;
	  							resolve(res.body.data[0]);
	  						}
	  					}
	  				);
	  			});
	  		},

	  		/**
	  		 * 获取用户在某个企业的用户信息
	  		 * @param  {Object} data 用户所在企业的企业信息，有getList方法获得
	  		 * @return {[type]}      [description]
	  		 */
	  		getItem (data) {
	  			console.log(data);
	  			this.$http.post(this.api.eab, {
	  				module: 'contact',
	  				action: 'getContactListByContactIds',
  					contactIds: [data.contactList[0].contactId],
                    enterpriseId: data.enterpriseId
	  			}, {showLoading: true}).then(
  					res => {
  						if (res.body.result === 1) {
  							this.user = res.body.data[0];
  						} else {
  							this.$toasted.show(res.body.errorMsg);
  						}
  					}
  				);
	  		}
	  	}

	};
</script>

<style lang="scss" scoped>
	@import '../scss/mixin.scss';

	.home-ctr{
		@include app-top-main-bottom(196px, 0);

		.header{
			width: 640px;
			height: 196px;
			padding: 10px 20px;
			box-sizing: border-box;
			background: url('../assets/bg_header.png') center center no-repeat;
			background-size: cover;
			color: #fff;

			h2{
				font-size: 34px;
				font-weight: normal;
				line-height: 2;
			}

			span{
				font-size: 28px;
			}
		}

	}
</style>
